//
//  BVErrorView.m
//  BVUIBizKit
//
//  Created by xiute on 2019/11/12.
//

#import "BVErrorView.h"

@interface BVErrorView ()

@property (nonatomic, copy) dispatch_block_t retryBlock;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *navTopMargin;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UIButton *actButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nvHeight;

@end


@implementation BVErrorView

- (void)awakeFromNib {
    [super awakeFromNib];
    if (isIPhoneX) {
        _navTopMargin.constant = 44;
    }else {
        _navTopMargin.constant = 20;
    }
}

+ (BVErrorView *)showWithType:(BVErrorViewType)type
                       inView:(UIView *)aView
                   retryBlock:(nonnull dispatch_block_t)retryBlock{
    NSBundle *bundle = [BVBundle bundleWithClass:[self class] name:BV_BUNDLE_UIBIZKIT_MODULE];
    BVErrorView *view = [bundle loadNibNamed:@"BVErrorView" owner:nil options:nil].firstObject;
    view.frame = aView.bounds;
    [aView addSubview:view];
    view.retryBlock = retryBlock;
    
    switch (type) {
        case BVErrorViewTypeHome: {
            view.textLabel.text = @"网络不给力，请再试试吧";
            [view.actButton setTitle:@"重   试" forState:UIControlStateNormal];
            view.imageView.image = [UIImage bv_imageNamed:@"empty_basketball" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_UIBIZKIT_MODULE]];
        }
            break;
        case BVErrorViewTypePlayground: {
            view.textLabel.text = @"无法搜索到相关信息";
            [view.actButton setTitle:@"重   试" forState:UIControlStateNormal];
            view.imageView.image = [UIImage bv_imageNamed:@"empty_playground" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_UIBIZKIT_MODULE]];
        }
            break;
        case BVErrorViewTypeFavorite: {
            view.textLabel.text = @"收藏一片空空，快去逛逛吧";
            [view.actButton setTitle:@"立即去逛逛" forState:UIControlStateNormal];
            view.imageView.image = [UIImage bv_imageNamed:@"empty_favorite" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_UIBIZKIT_MODULE]];
        }
            break;
        case BVErrorViewTypePersonal: {
            view.textLabel.text = @"暂无数据，请稍后再试";
            [view.actButton setTitle:@"重   试" forState:UIControlStateNormal];
            view.imageView.image = [UIImage bv_imageNamed:@"empty_basketball" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_UIBIZKIT_MODULE]];
        }
            break;
        case BVErrorViewTypeNoData: {
            view.textLabel.text = @"暂无数据，请稍后再试";
            [view.actButton setTitle:@"重   试" forState:UIControlStateNormal];
            view.imageView.image = [UIImage bv_imageNamed:@"empty_basketball" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_UIBIZKIT_MODULE]];
            view.navTopMargin.constant = 0;
            view.nvHeight.constant = 0;
        }
            
        default:
            break;
    }
    
    return view;
}
- (IBAction)clickButtonAct:(id)sender {
    [self removeFromSuperview];
    if (self.retryBlock) {
        self.retryBlock();
    }
}

@end
